<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="" prop="directory_id">
            <el-cascader
              v-model="temp.directory_id"
              :options="treeList"
              :props="treeProps"
              :show-all-levels="false"
              label="name"
              change-on-select
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属数据源" prop="sourceId">
            <el-input v-model="temp.sourceId" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="表名" prop="tableName">
            <el-input v-model="temp.tableName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="表注释" prop="tableComment">
            <el-input v-model="temp.tableComment" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任人" prop="person">
            <el-input v-model="temp.person" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任人电话" prop="phone">
            <el-input v-model="temp.phone" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="描述" prop="description">
            <el-input v-model="temp.description" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="temp.remarks" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="目录id" prop="directoryId">
            <el-input v-model="temp.directoryId" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus==='create'?createData():updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createMasterdataTable, updateMasterdataTable, getMasterdataTable } from '@/api/masterdata/masterdataTable'

export default {
  name: 'MasterdataTableForm',
  data() {
    return {
      rules: {
      },
      temp: {},
      treeList: [],
      treeProps: {
        value: 'id',
        label: 'name',
        expandTrigger: 'hover'
      },
      textMap: {
        update: '编辑',
        create: '新建'
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  methods: {
    setList(treeList) {
      this.treeList = treeList
    },
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
              sourceId: undefined,
              tableName: undefined,
              tableComment: undefined,
              person: undefined,
              phone: undefined,
              description: undefined,
              elementType: undefined,
              createDate: undefined,
              updateDate: undefined,
              releaseStatus: undefined,
              delFlag: undefined,
              tenantId: undefined,
              remarks: undefined,
              createBy: undefined,
              updateBy: undefined,
              directoryId: undefined
            }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      if (this.temp.directory_id !== undefined && this.temp.directory_id !== '' && this.temp.directory_id instanceof Array) {
        this.temp.directory_id = this.temp.directory_id[this.temp.directory_id.length - 1]
      }

      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createMasterdataTable(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getMasterdataTable(id).then(response => {
        if (response.data.code === 0) {
          this.temp = response.data.data
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.temp.directory_id !== undefined && this.temp.directory_id !== '' && this.temp.directory_id instanceof Array) {
            this.temp.directory_id = this.temp.directory_id[this.temp.directory_id.length - 1]
          }

          this.loading = true
          updateMasterdataTable(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
